package tmt.usercenter.web.controller.admin;


import com.tmt.datatable.editor.EditorModel;
import com.tmt.datatable.editor.EditorValid;
import com.tmt.datatable.ext.controller.DataTableController;
import com.tmt.datatable.ext.controller.SelectOptions;
import com.tmt.datatable.ext.controller.TablePage;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tmt.usercenter.web.common.Const;
import tmt.usercenter.web.domain.ClientSystem;
import tmt.usercenter.web.service.ClientSystemService;
import tmt.usercenter.web.vo.TreeNodeVo;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Getter
@Setter
@Controller
@RequestMapping(Const.ADMIN_PATH + "/client")
@TablePage(Const.ADMIN_PATH + "/client/client-index")
public class ClientSystemController extends DataTableController<ClientSystem, Long> {

    @Autowired
    private ClientSystemService clientSystemService;

    @Override
    public EditorModel<ClientSystem> doTableAction(@Valid @EditorValid EditorModel<ClientSystem> editor) throws ExecutionException, InterruptedException {
        return executeTableAction(editor, new String[]{"creator"}).get();
    }

    @ResponseBody
    @RequestMapping(value = "/scopetree")
    public List<TreeNodeVo> scopetree(HttpServletRequest request,
                                      @RequestParam(value = "clientId", required = false) String clientId) {
        List<TreeNodeVo> scopeList = clientSystemService.findScopeTree(clientId);
        return scopeList;
    }

    @ResponseBody
    @RequestMapping("/scope/select2")
    public SelectOptions queryScopeForSelect2(HttpServletRequest request, @RequestParam(value = "search", required = false) String search,
                                              @RequestParam(value = "page", defaultValue = "0") Integer pageIndex,
                                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        return clientSystemService.findScopeOptionsByFieldNameAndAnnotation();
    }

    @ResponseBody
    @RequestMapping("/resourceIds/select2")
    public SelectOptions queryResourceIdsForSelect2(HttpServletRequest request, @RequestParam(value = "search", required = false) String search,
                                                    @RequestParam(value = "page", defaultValue = "0") Integer pageIndex,
                                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                    @RequestParam(value = "scope", required = false) String scope) {
        return clientSystemService.findResourceIdOptionsByFieldNameAndAnnotation(scope);
    }

    @ResponseBody
    @RequestMapping("/grantTypes/select2")
    public SelectOptions queryGrantTypesForSelect2(HttpServletRequest request, @RequestParam(value = "search", required = false) String search,
                                                   @RequestParam(value = "page", defaultValue = "0") Integer pageIndex,
                                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return clientSystemService.findSelectOptionsForAuthorizedGrantTypes();
    }

}
